package com.blog.servlet;

import java.io.IOException;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.blog.dao.ArticleDao;
import com.blog.dao.CategoryDao;
import com.blog.dao.ReplayDao;
import com.blog.dto.Article;
import com.blog.dto.Category;
import com.blog.dto.Replay;

public class IndexServlet extends HttpServlet {

	public void initAllInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//获取所有文章
		Vector<Article> vArticle=new Vector<Article>();
		ArticleDao articleDao=new ArticleDao();
		vArticle = articleDao.findAllArticle();
		HttpSession session = req.getSession();
		session.setAttribute("allArticle", vArticle);
		
		//获取所有分类
		CategoryDao categoryDao = new CategoryDao();
		Vector<Category> category=new Vector<Category>();
		category = categoryDao.findAllCategory();
		session.setAttribute("allCategory", category);
		
		//获取所有评论
		Vector<Replay> vReplays = new Vector<Replay>();
		ReplayDao replayDao = new ReplayDao();
		vReplays = replayDao.findAllReplay();
		session.setAttribute("allReplay", vReplays);
		
		return ;
		
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String act = null;
		act=req.getParameter("act");
		if(act==null || act.equals("")) act = "index";
		System.out.println("IndexServlet act = "+act);
		
		switch (act) {
		case "findUserByName":
			
			break;
		default:
			initAllInfo(req, resp);
//			页面重定向
//	 	    resp.sendRedirect(req.getContextPath()+"/home/index.jsp");
//			页面转发
			req.getRequestDispatcher("/home/index.jsp").forward(req, resp);
			break;
		}
	}
	
}
